import frappe
import pandas as pd
import numpy as np

@frappe.whitelist()
def get_work_plan_report():
    data = frappe.db.get_all('work_plan',fields=['name','project', 'work_person','work_person_name','start_dt', 'end_dt'],as_list=False)
    df = pd.DataFrame.from_records(data)
    # 将日期列转换为日期时间类型
    df['start_datetime'] = pd.to_datetime(df['start_dt'])
    df['end_datetime'] = pd.to_datetime(df['end_dt'])
    df['parent'] = df['work_person']
    print(df.head())
    result = df.groupby(['work_person','work_person_name']).agg({'start_datetime': 'min', 'end_datetime': 'max'}).reset_index() 
    result['start_dt'] = result['start_datetime'].dt.date.fillna('') 
    result['end_dt'] = result['end_datetime'].dt.date.fillna('')  
    result['name'] = result['work_person']
    result['project'] = result['work_person_name']
    result['parent'] = ''
    print(result.head())   
    # 使用 pd.concat() 进行并集操作
    union_df = pd.concat([df, result])
    frappe.response['data'] = union_df.to_dict('records')